package com.dhc.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

//加此注解job即为有状态的，即可实现数据的持久化
//@PersistJobDataAfterExecution
public class JobCronTrigger implements org.quartz.Job {
    //声明传递过来的key值,并为其设置set方法,quartz会自动调用set方法为其赋值
    private String jobMsg;
    private String triggerMsg;
    //判断是否为持久化，当不加注解时每次执行此任务都会重新初始化值
    private Integer count;

    public void setJobMsg(String jobMsg) {
        this.jobMsg = jobMsg;
    }

    public void setTriggerMsg(String triggerMsg) {
        this.triggerMsg = triggerMsg;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        System.out.println("执行了");
    }

    public static void main(String[] args) throws Exception{
        //Scheduler调度器 工厂中获取调度实例
        Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
        //Job任务实例
        JobDetail jobDetail = JobBuilder.newJob(JobCronTrigger.class)
                .withIdentity("job1", "jobGroup1")//1任务名2组名
                .build();
        //Trigger触发器
        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1", "triggerGroup1")
                .startNow()//马上启动触发器
                .withSchedule(CronScheduleBuilder.cronSchedule("* * * * * ?"))
                .build();
        //让调度器关联任务和触发器
        scheduler.scheduleJob(jobDetail,trigger);
        //启动
        scheduler.start();
    }
}
